
package com.jf.cloud.live.controller.multishop;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.live.param.LiveUsableNumParam;
import com.jf.cloud.live.constant.LiveInterfaceType;
import com.jf.cloud.live.dto.LiveRoomProdDTO;
import com.jf.cloud.live.model.LiveRoomProd;
import com.jf.cloud.live.service.LiveLogService;
import com.jf.cloud.live.service.LiveRoomProdService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 直播商品
 *
 * @author zz
 * @date 2022-08-17 17:09:06
 */
@RestController("multishopLiveRoomProdController")
@RequestMapping("/m/live_room_prod")
@Tag(name = "商家端直播间商品接口")
public class LiveRoomProdController {

    @Autowired
    private LiveRoomProdService liveRoomProdService;
    @Autowired
    private LiveLogService liveLogService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取直播商品列表" , description = "分页获取直播商品列表")
    public ServerResponseEntity<PageVO<LiveRoomProd>> page(@Valid PageDTO pageDTO, @ParameterObject LiveRoomProdDTO liveRoomProdDTO) {
        PageVO<LiveRoomProd> liveRoomProdPage = liveRoomProdService.page(pageDTO, liveRoomProdDTO);
        return ServerResponseEntity.success(liveRoomProdPage);
    }

    @GetMapping
    @Operation(summary = "获取直播商品" , description = "根据roomProdId获取直播商品")
    public ServerResponseEntity<LiveRoomProd> getByRoomProdId(@ParameterObject @RequestParam Long roomProdId) {
        return ServerResponseEntity.success(liveRoomProdService.getByRoomProdId(roomProdId));
    }

    @PostMapping
    @Operation(summary = "保存直播商品" , description = "保存直播商品")
    public ServerResponseEntity<Void> save(@Valid @RequestBody LiveRoomProdDTO liveRoomProdDTO) {
        LiveRoomProd liveRoomProd = mapperFacade.map(liveRoomProdDTO, LiveRoomProd.class);
        liveRoomProd.setRoomProdId(null);
        liveRoomProdService.save(liveRoomProd);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新直播商品" , description = "更新直播商品")
    public ServerResponseEntity<Void> update(@Valid @RequestBody LiveRoomProdDTO liveRoomProdDTO) {
        LiveRoomProd liveRoomProd = mapperFacade.map(liveRoomProdDTO, LiveRoomProd.class);
        liveRoomProdService.update(liveRoomProd);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除直播商品" , description = "根据直播商品id删除直播商品")
    public ServerResponseEntity<Void> delete(@RequestParam Long roomProdId) {
        liveRoomProdService.deleteById(roomProdId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/getAddRoomProdNum")
    @Operation(summary = "查询商家今日剩余可用添加直播间商品次数")
    public ServerResponseEntity<LiveUsableNumParam> getAddRoomNum() {
        return ServerResponseEntity.success(liveLogService.getLiveNum(AuthUserContext.get().getTenantId(), LiveInterfaceType.IMPORT_ROOM_PROD));
    }
}
